﻿using System;
using System.IO;
/// <summary>
/// Summary description for MyIO
/// </summary>
/// 
namespace GG.InputOut
{
    public class MyIO
    {
        public MyIO()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public string ReadFromFile(string filePath)
        {
            StreamReader _reader = null;
            string _data = null;
            try
            {
                _reader = new StreamReader(filePath, true);
                _data = _reader.ReadToEnd();
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
            finally
            {
                _reader.Dispose();
                _reader = null;
            }

            return _data;
        }

        public string GetFileInformation(string filePath)
        {
            FileInfo _file = null;
            string _fileinfo = null;
            try
            {
                _file = new FileInfo(filePath);
                _fileinfo = _file.Attributes.ToString();
                _fileinfo += ",Creation Time: " + _file.CreationTime.ToString();
                _fileinfo += ",Last Access Time: " + _file.LastAccessTime.ToString();
                _fileinfo += ",File Length : " + _file.Length.ToString() + " Bytes";
                _fileinfo += ",Extension : " + _file.Extension.ToString();
                _fileinfo += ",Is Read Only : " + _file.IsReadOnly.ToString();
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
            finally
            {
                _file = null;
            }

            return _fileinfo;
        }

        public bool CheckDirectory(string filePath)
        {
            bool _retvalue = false;
            DirectoryInfo _dirinfo = null;
            try
            {
                _dirinfo = new DirectoryInfo(filePath);
                _retvalue = _dirinfo.Exists;
            }
            catch (Exception Ex)
            {
                Ex = null;
                _retvalue = false;
            }
            finally
            {
                _dirinfo = null;
            }
            return _retvalue;
        }

        public bool CreateDirectory(string filePath, string DirName)
        {
            bool _retValue = false;
            DirectoryInfo _dirinfo = null;
            DirectoryInfo _newDir = null;
            try
            {
                _dirinfo = new DirectoryInfo(filePath + DirName);
                if (_dirinfo.Exists)
                {
                    _retValue = false;
                }
                else
                {
                    _dirinfo = new DirectoryInfo(filePath);
                    if (_dirinfo.Exists)
                    {
                        _newDir = _dirinfo.CreateSubdirectory(DirName);
                        _retValue = _newDir.Exists;
                    }
                    else
                    {
                        _retValue = false;
                    }
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
                _retValue = false;
            }
            finally
            {
                _dirinfo = null;
                _newDir = null;
            }

            return _retValue;
        }

        public bool WriteNewFile(string filePath, string fileName, string[] fileContent)
        {
            StreamWriter _writer = null;
            try
            {
                _writer = new StreamWriter(filePath + fileName);
                for (int l = 0; l < fileContent.Length; l++)
                {
                    _writer.WriteLine(fileContent[l]);
                }
                if (_writer != null)
                {
                    _writer.Flush();
                    _writer.Close();
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
            finally
            {
                if (_writer != null)
                {
                    //_writer.Flush();
                    //_writer.Dispose();
                    _writer = null;
                }
            }
            return true;
        }

    }
}
